<template>
  <div>
    <ul class="tags" v-if="showTags">
      <li
        class="tags-li"
        v-for="(item, index) in tagsList"
        :class="{ active: isActive(item.path) }"
        :key="index"
      >
        <router-link :to="item.path" class="tags-li-title">
          {{ item.title }}
        </router-link>
        <span class="tags-li-icon" v-if="tagsList.length>1" @click="closeTags(index)"
          ><el-icon><Close /></el-icon
        ></span>
      </li>
    </ul>
    <div class="tags-close-box">
      <el-dropdown>
        <el-button size="small" type="primary">
          任务选项<el-icon class="el-icon--right">
            <arrow-down />
          </el-icon>
        </el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="other" @click="handleTags('other')"
              >关闭其他</el-dropdown-item
            >
            <el-dropdown-item command="all" @click="handleTags('all')"
              >关闭所有</el-dropdown-item
            >
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, onMounted, computed, inject } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
import bus from "@/components/nav/bus";

const store = useStore();
const route = useRoute();
const router = useRouter();
// const bus = inject("bus"); // 假设bus是通过provide在顶层注入的
let tagsList = ref([]);

//激活新路由tags 功能正常
const isActive = (path) => {
  //激活后，设置tags
  setTags(route);
  return path === route.fullPath;
};
// 关闭单个标签
const closeTags = (index) => {
  //获取要删除标签的对象
  const delItem = tagsList.value.splice(index, 1)[0];  
  const item = tagsList.value[index]
    ? tagsList.value[index]
    : tagsList.value[index - 1];
  if (item) {
    //剩余tags不为空
    if (delItem.path === route.fullPath) {
      router.push(item.path);
    }
  } else {
    //剩余为空
    router.push("/");
    setTags(route);
  }
};
// 关闭全部标签
const closeAll = () => {
  tagsList.value = [{ title: '管理首页', path: '/dashboard', name: '管理首页' }];
  router.push("/");
  
};

// 关闭其他标签 功能正常
const closeOther = () => {
  //检索当前打开的页面路由
  const curItem = tagsList.value.filter((item) => {
    return item.path === route.fullPath;
  });
  //判断当前打开的路由对象保存，其他清空
  tagsList.value = curItem ? [curItem[0]] : [];
};

// 设置标签 功能正常
const setTags = (route) => {
  const isExist = tagsList.value.some((item) => {
    return item.path === route.fullPath;
  });
  if (!isExist) {
    if (tagsList.value.length >= 8) {
      tagsList.value.shift();
    }
    const name = route.matched[1]?.name; // 使用可选链操作符

    tagsList.value.push({
      title: route.meta.title,
      path: route.fullPath,
      name: name,
    });
  }
  bus.emit("tags", tagsList.value);
};
const handleTags = (command) => {
  command === "other" ? closeOther() : closeAll();
};

//显示标签
let showTags = computed(() => {
  return tagsList.value.length > 0;
});

// 使用 watch 函数来观察 route 的变化
watch(
  () => route, // 被观察的响应式引用
  (newValue, oldValue) => {
    //如果路由地址变化，则设置新的tags
    setTags(newValue);
  }
);

onMounted(() => {
  //设置tags
  setTags(route);
  // 监听关闭当前页面的标签页
  bus.on("close_current_tags", () => {
    for (let i = 0, len = tagsList.value.length; i < len; i++) {
      const item = tagsList.value[i];
      if (item.path === route.fullPath) {
        if (i < len - 1) {
          router.push(tagsList.value[i + 1].path);
        } else if (i > 0) {
          router.push(tagsList.value[i - 1].path);
        } else {
          router.push("/");
        }
        tagsList.value.splice(i, 1);
        break;
      }
    }
  });
});
</script>


<style>
.tags {
  position: relative;
  height: 30px;
  overflow: hidden;
  background: #fff;
  padding-right: 120px;
  box-shadow: 0 5px 10px #ddd;
  margin-bottom: 15px;
}

.tags ul {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
}

.tags-li {
  float: left;
  margin: 3px 5px 2px 3px;
  border-radius: 3px;
  font-size: 12px;
  overflow: hidden;
  cursor: pointer;
  height: 23px;
  line-height: 23px;
  border: 1px solid #e9eaec;
  background: #fff;
  padding: 0 5px 0 12px;
  vertical-align: middle;
  color: #666;
  -webkit-transition: all 0.3s ease-in;
  -moz-transition: all 0.3s ease-in;
  transition: all 0.3s ease-in;
}

.tags-li:not(.active):hover {
  background: #f8f8f8;
}

.tags-li.active {
  color: #fff;
  border: 1px solid #409eff;
  background-color: #409eff;
}

.tags-li-title {
  float: left;
  max-width: 80px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  margin-right: 5px;
  color: #666;
}

.tags-li.active .tags-li-title {
  color: #fff;
}

.tags-close-box {
  position: absolute;
  right: 0;
  top: 0;
  box-sizing: border-box;
  padding-top: 1px;
  text-align: center;
  width: 110px;
  height: 30px;
  background: #fff;
  box-shadow: -3px 0 15px 3px rgba(0, 0, 0, 0.1);
  z-index: 10;
}
</style>
